package cn.liwq.demo.java.raw.annotation;

import cn.liwq.demo.java.raw.annotation.anno.Note;
import cn.liwq.demo.java.raw.annotation.entry.NoteFoo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * <p>Project: demo-common </p>
 * 
 * <p>Function:  </p>
 * 
 * <p>Description:  </p>
 * 
 * <p>Copyright: Copyright(c) 2018 Ucarinc </p>
 * 
 * <p>Company: Ucarinc </p>
 * 
 * @author weiqiang.li@ucarinc.com
 * 
 * @date 2018-08-16 10:19:11
 * 
 * @version 1.0
 */
public class AnnotationDemo {

    public static void main(String[] args) {
        // class annotation
        Note noteClsAnno = NoteFoo.class.getAnnotation(Note.class);
        output(noteClsAnno);

        // field annotation
        Field[] fs = NoteFoo.class.getDeclaredFields();
        for (Field f : fs) {
            if (!f.isAnnotationPresent(Note.class)) {
                continue;
            }
            output(f.getAnnotation(Note.class));
        }

        Method[] ms = NoteFoo.class.getDeclaredMethods();
        for (Method m : ms) {
            // param annotation
            Parameter[] ps = m.getParameters();
            for (Parameter p : ps) {
                if (!p.isAnnotationPresent(Note.class)) {
                    continue;
                }
                Note noteParamAnno = p.getAnnotation(Note.class);
                output(noteParamAnno);
            }
            // method annotation
            if (!m.isAnnotationPresent(Note.class)) {
                continue;
            }
            Note noteMethodAnnotation = m.getAnnotation(Note.class);
            output(noteMethodAnnotation);

        }
    }

    private static void output(Note n) {
        System.out.println(String.format("value : %s, tags : %s", n.value(), Arrays.stream(n.tags()).collect(
                Collectors.joining(", "))));
    }
}
